package com.page;

import com.utils.LogCustom;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;

import java.lang.management.OperatingSystemMXBean;
import java.net.MalformedURLException;
import java.net.URL;

public class TestBaseCase {
    public static WebDriver driver;
    //方法描述
    public LogCustom logCustom = new LogCustom(this.getClass().getSuperclass());

    @BeforeTest
    @Parameters({"driver", "nodeURL", "isMaximize"})
    public void setup(String driver, String nodeURL, Boolean isMaximize) throws MalformedURLException {
        logCustom.info("------------------开始执行测试---------------");
        if (nodeURL.equals("") || nodeURL.isEmpty()) {
            logCustom.info("读取testng.xml配置的" + driver + "浏览器并将其初始化\n");
            try {
                this.driver = setDriver(driver);
            } catch (Exception e) {
                logCustom.error("没有成功配置浏览器环境");
                e.printStackTrace();
            }
            System.out.println(nodeURL);
        } else {
            logCustom.info("读取xml配置：浏览器:" + driver + "；gridNodeURL:" + nodeURL);
            try {
                this.driver = setRemoteDriver(driver, nodeURL);
            } catch (Exception e) {
                logCustom.error("没有成功浏览器环境配置错误:" + e.getMessage());
            }
        }
        if (isMaximize) {
            this.driver.manage().window().maximize();
        }
    }

    @AfterTest
    public void tearDown() {
//        this.driver.close();
//        this.driver.quit();
        logCustom.info("-------------结束测试，并关闭退出浏览器-------------");
    }

    /**
     * 用枚举类型列出浏览器列表，用于设置浏览器类型的函数参数
     *
     * @author zheng
     */
    private WebDriver setDriver(String browsername) {

        switch (browsername) {
            case "FirefoxDriver":
                System.setProperty("webdriver.firefox.bin", "C:/Program Files (x86)/Mozilla Firefox/firefox.exe");
                FirefoxProfile firefoxProfile = new FirefoxProfile();
                this.driver = new FirefoxDriver(firefoxProfile);
                break;
            case "ChromeDriver":
//                System.setProperty("webdriver.chrome.driver", "src\\main\\resources\\chromedriver.exe");
//                System.setProperty("webdriver.chrome.driver", "\\pplications\\Google Chrome.app\\Contents/MacOS\\Google Chrome");
                ChromeOptions options=new ChromeOptions();
                options.addArguments("disable-infobars");
                this.driver = new ChromeDriver(options);
                break;
            case "InternetExplorerDriver":
                System.setProperty("webdriver.ie.driver", "src\\main\\resources\\IEDriverServer.exe");
                DesiredCapabilities dc = DesiredCapabilities.internetExplorer();
                dc.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
                dc.setCapability("ignoreProtectedModeSettings", true);
                this.driver = new InternetExplorerDriver(dc);
                break;
            case "HtmlUnitDriver":
                this.driver = new HtmlUnitDriver();
                break;
            default:
                this.driver = new FirefoxDriver();
                break;
        }
        return driver;
    }


    private WebDriver setRemoteDriver(String browsername, String nodeURL) throws MalformedURLException {
        switch (browsername) {
            case "FirefoxDriver":
                DesiredCapabilities capabilities = DesiredCapabilities.firefox();
                capabilities.setBrowserName("firefox");
                capabilities.setPlatform(Platform.WINDOWS);
                //driver= new RemoteWebDriver(new URL("http://192.168.0.205:4455/wd/hub"), capabilities);
                driver = new RemoteWebDriver(new URL(nodeURL), capabilities);
                break;
            case "ChromeDriver":
                // System.setProperty("webdriver.chrome.driver", "E:\\autotest\\autotmaiton\\resource\\chromedriver.exe");
                //driver=new ChromeDriver();
                DesiredCapabilities dcchorme = DesiredCapabilities.chrome();
                dcchorme.setBrowserName("chrome");
//                dcchorme.setVersion("46.0.2490.86 m");
                dcchorme.setVersion("65.0.3325.146");
                dcchorme.setPlatform(Platform.WINDOWS);
                driver = new RemoteWebDriver(new URL(nodeURL), dcchorme);
                break;
            case "InternetExplorerDriver-8":
                DesiredCapabilities dc = DesiredCapabilities.internetExplorer();
                dc.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
                dc.setCapability("ignoreProtectedModeSettings", true);
                dc.setBrowserName("internet explorer");
                dc.setVersion("8.0.6001.18702");
                dc.setPlatform(Platform.XP);
                driver = new RemoteWebDriver(new URL(nodeURL), dc);
                break;
            case "InternetExplorerDriver-9":
                DesiredCapabilities dc2 = DesiredCapabilities.internetExplorer();
                dc2.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
                dc2.setCapability("ignoreProtectedModeSettings", true);
                dc2.setBrowserName("internet explorer");
                dc2.setVersion("9.0.8112.16421");
                dc2.setPlatform(Platform.WINDOWS);
                driver = new RemoteWebDriver(new URL(nodeURL), dc2);
                //driver=new InternetExplorerDriver(dc2);
                break;
            case "HtmlUnitDriver":
                this.driver = new HtmlUnitDriver();
                break;
            default:
                this.driver = new FirefoxDriver();
                break;
        }
        return driver;
    }

    public static void main(String args[]) {
//        WebDriver driver2 = new FirefoxDriver();
//        driver2.get("http://www.baidu.com");

        ChromeOptions options = new ChromeOptions();
        options.addArguments("disable-infobars");
        options.addArguments("user-data-dir=/Users/yangliang/Library/Application Support/Google/Chrome/Default");
        WebDriver driver2 = new ChromeDriver(options);
        driver2.get("https://hsh.vpclub.cn/web/#/Activities/Train/Home/1083544069123923968");
    }


}
